import api from './config'

export interface LoginRequest {
  username: string
  password: string
}

export interface LoginResponse {
  code: number
  msg: string
  data: {
    id: number
    token: string
  }
}

export interface User {
  id: number
  username: string
  password: string
  email: string
  phone: string
  createTime: string
  updateTime: string
  isDeleted: number
}

// 注册请求与响应
export interface RegisterRequest {
  username: string
  password: string
}

export interface RegisterResponse {
  code: number
  msg: any
  data: boolean
}

// 个人信息
export interface UserInfo {
  id: number
  username: string
  password: string
  email: string | null
  phone: string | null
  address?: string | null
  avatarUrl?: string | null
  createTime: number[] | string
  updateTime: number[] | string
  isDeleted: number
}

export interface UserInfoResponse {
  code: number
  msg: any
  data: UserInfo
}

// 头像上传响应
export interface UploadAvatarResponse {
  code: number
  msg: any
  data: string // 头像URL
}
 
// 获取头像响应
export interface GetAvatarResponse {
  code: number
  msg: any
  data: string // 头像URL
}
 
 export const userApi = {
   login: async (data: LoginRequest): Promise<any> => {
     return api.post('/user/login', data)
   },
   // 新增：用户注册
   register: async (data: RegisterRequest): Promise<RegisterResponse> => {
     return api.post('/user/register', data)
   },
   // 新增：获取个人信息
   getUserInfo: async (): Promise<UserInfoResponse> => {
     return api.get('/user/user/UserInfo')
   },
   // 新增：获取用户头像
   getAvatar: async (): Promise<GetAvatarResponse> => {
     return api.get('/user/avatar')
   },
   // 新增：上传头像
   uploadAvatar: async (file: File): Promise<UploadAvatarResponse> => {
     const formData = new FormData()
     formData.append('file', file)
     return api.post('/user/common/uploadUserAvatar', formData, {
       headers: {
         'Content-Type': 'multipart/form-data'
       }
     })
   }
 }
 
